package com.example.liverock.androidalltest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class CheckButtonActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_button);


        // 单选组
        final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);

        //在改变单选按钮组的值时获取
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton sex = (RadioButton) findViewById(checkedId);
                Toast.makeText(CheckButtonActivity.this, sex.getText().toString(), Toast.LENGTH_SHORT).show();
            }
        });

        Button commit = (Button)findViewById(R.id.commit);

        //  单机其他按钮时获取
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < radioGroup.getChildCount(); i++) {
                    //根据索引值获取单选按钮
                    RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
                    if (radioButton.isChecked()) {
                        Toast.makeText(CheckButtonActivity.this, radioButton.getText().toString(), Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            }
        });

        /**
         *  功能：实现选中复选框后，“开始”按钮才可用，否则为不可用状态；
         */

        //复选框；
        CheckBox  checkBox = (CheckBox)findViewById(R.id.checkBox);
        final ImageButton imageButton = (ImageButton)findViewById(R.id.start);

        //  复选框的用法；
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                {
                    // 图片按钮可见
                    imageButton.setVisibility(View.VISIBLE);
                }
                else
                {
                    // 图片按钮不可见
                    imageButton.setVisibility(View.INVISIBLE);
                }
                imageButton.invalidate();
            }
        });

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(CheckButtonActivity.this, "进入游戏...", Toast.LENGTH_SHORT).show();
            }
        });

    }

}
